<template>
  <view class="tuik-tip">
    取消政策：下单成功后，门店会为您保留寄存空间。寄存当天， 因自身原因取消订单，收取订单金额20%取消费。
  </view>
  
</template>

<script>
export default {
  name: 'zbTitle',
  props:{
    list:Array,
  },
  data() {
  	return {
      uploading:false,
      lists:[]
  	}
  },
  mounted() {
    this.lists = this.list || [];
    console.log('this.list===>',this.list)
  },
  methods: {
    downImgClick(){
      this.lists.forEach(n => {
        console.log(n.fullurl)
        this.downImg(n.fullurl)
      })
    },
    downImg(url) {
      console.log('urlurlurlurlurlurlurlurl=====>',url)
      if(!url){
        console.log('图片获取失败，请稍后再试')
        return;
      }
      wx.showLoading({
        title: '加载中...',
      })
      //图片保存到本地
      wx.downloadFile({
        url: url,//图片地址
        success (downres) {
          // 保存图片到相册
          wx.saveImageToPhotosAlbum({
            filePath: downres.tempFilePath,
            success(res) {
              wx.hideLoading()
              console.log('保存成功')
            },
            fail(err){
              wx.hideLoading()
              if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
                // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
                wx.showModal({
                  title: '提示',
                  content: '请授权保存到相册',
                  showCancel: false,
                  success() {
                    wx.openSetting({
                      success(openres) {
                        console.log("openres", openres)
                        if (openres.authSetting['scope.writePhotosAlbum']) {
                          console.log('获取权限成功，再次点击图片即可保存')
                        } else {
                          console.log('获取权限失败，无法保存到相册哦~')
                        }
                      },
                      fail(failerr) {
                        console.log("failerr", failerr)
                      }
                    })
                  }
                })
              }
            }
          })
        },
        fail() {
          wx.hideLoading()
          console.log('下载失败，请稍后再试')
        }
      })
    },
    previewImg(e, url){
      const list = this.lists.map(m=>{
        return m.url
      })
      uni.previewImage({
        urls: list,
        longPressActions: {
          itemList: ['发送给朋友', '保存图片', '收藏'],
          success: function(data) {
            console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
          },
          fail: function(err) {
            console.log(err.errMsg);
          }
        }
      });
      
    },
    // 选择图片并上传
    uploadImgs(){
      if(this.lists.length < 2){
        const that = this;
        uni.chooseImage({
          count:2,
        	success: (res) => {
        		const tempFilePaths = res.tempFilePaths;
            tempFilePaths.forEach(n=>{
              that.upAjax(n)
              // const time = new Date().getTime()
              // this.lists = this.lists.concat({url:n, fullurl:n, id:time});
              // this.$emit('bakUrl',this.lists)
            })
        		
        	},
        });
      }else{
        uni.showToast({
          title:'最多上传两张图片',
          icon:'none'
        })
      }
    },
    close(url){
      this.lists = this.lists.filter(n => n.url !== url)
    },
    upAjax(url){
      const time = new Date().getTime()
      this.uploading=true
      uni.uploadFile({
      	url: this.$api.uploadImageApi, //仅为示例，非真实的接口地址
      	filePath: url,
      	name: 'file',
      	success: (res) => {
          res = JSON.parse(res.data);
          console.log('res=======>',res)
          if(res.code === 200){
            this.lists = this.lists.concat({url:res.data.file_path_all, id:time});
            // const files = this.lists.map(m => {
            //   return m.url
            // });
            // console.log('files====>',files)
            this.$emit('getImages',this.lists)
          }
          
      	},
        complete:res=>{
          this.uploading=false
        }
      });
    }
  }
  	
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.tuik-tip{font-size: 26rpx; color: #666;}
</style>
